Add a MCP9808 Temperature Sensor to a Microcontroller

For CircuitPython Capable Microcontroller Board Types.

Power down your MCU before you connect something onto the GPIO pins!

First things first, how to get your code onto a Microcontroller?

A lot of boards have an USB connector and a couple of MCU boards behave like a USB drive when you connect them to a computer. For example:

  • Adafruit Trinket M0
  • Adafruit ItsyBitsy M0 and M4 Express
  • Adafruit Feather M4 Express
And probably a couple more.

It is very simple, connect your MCU via a data USB cable to a computer. It will propagate it self as a USB drive. From there you can simply drag/drop or copy/past your code onto the microcontroller. Although you have to put you code into a Python file named main.py and put that file in the root of the drive/system.


Download Libraries

Before you can read your sensor data, you have to install at least one library. And that will be the adafruit_bus_device library folder. Into this folder are three files; one init file and two others for I2C and SPI. So go and download the libraries from the link at the bottom of this page. And copy/past the adafruit_bus_device folder into your MCU lib folder. If you want to connect a sensor you have to install a library which belongs to that sensor as well. This time it is a file with a .mpy extension. Look for a file with the sensors name in it. And put this file also into the lib folder.

So I downloaded:

  • adafruit_bus_device folder
  • adafruit_mcp9808.mpy
And put them both into the lib folder in the root of your MCU. If the lib folder doesn't exists, create it.


Check sensor address

We want to know if we wired the sensor correctly. We are going to do a scan to see if the sensor is detected and if it is, print out it's address. So we will put a simple piece of code into main.py. Please note that this code will output all found addresses, so if you have multiple I2C devices connected, this code will output all their addresses. If you want to make sure you only see your new I2C device, disconnect all others.
To see the output of this little piece of Python code, connect with the serial console. # CircuitPython - I2C scan import time import board import busio i2c = busio.I2C(board.SCL, board.SDA) while not i2c.try_lock(): pass while True: print("I2C addresses found:", [hex(device_address) for device_address in i2c.scan()]) time.sleep(2) In Windows-10, look in the Device Manager to see which COM port your MCU uses. Use that in combination with a baudrate of 115200 to connect to your MCU.


Run that code!

You can now run one of the (many in some cases) example scripts Adafruit has written for you. Check out the examples for your library by visiting the repository for the library and looking in the example folder. In this case, it would be https://github.com/adafruit/Adafruit_CircuitPython_MCP9808/tree/master/examples

import time import board import busio import adafruit_mcp9808 # This example shows how to get the temperature from a MCP9808 board i2c_bus = busio.I2C(board.SCL, board.SDA) mcp = adafruit_mcp9808.MCP9808(i2c_bus) while True: tempC = mcp.temperature tempF = tempC * 9 / 5 + 32 print('Temperature: {} C {} F '.format(tempC, tempF)) time.sleep(2) Check the output by connecting to your MCU via the serial console.


Please note the addresses of the MCP9808

The MCP9808 can have eight different addresses. Which means you can connect up to eight MCP9808's on the same I2C bus! What does that mean for the Python code? For some odd reason someone thought it was nice to use decimal instead of hexadecimal as addresses?! So the code for the default address of 0x18 is: mcp = adafruit_mcp9808.MCP9808(i2c_bus) For all other addresses you should use the following code: mcp = adafruit_mcp9808.MCP9808(i2c_bus, address=25) This is the second address 0x19Hex = 25 decimal.

For more information about the addresses and how you can config your MCP9808, see the Sensor Info page for the MCP9808.


CircuitPython Library Bundle

The latest CircuitPython Libray Bundle Release from the CircuitPython.org site.